package priv.mill.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import priv.mill.model.Book;

@Controller
@RequestMapping("/books")
@SessionAttributes(names= {"books"})//names= {"books"}就表示books进入sesstion
//第二种写法：@SessionAttributes(types= {ArrayList.class})
public class BooksController {
	
	private static List<Book> books=new ArrayList<>();
	
	{
		books.add(new Book("神雕侠侣",1.11,"渣渣"));
		books.add(new Book("绝代双娇",2.11,"古龙"));
		books.add(new Book("隋唐英雄",3.11,"黄奕"));
	}
	
	@GetMapping("all")
	public String findAll(Map<String, Object> map) {
		map.put("book", books);//相当于进了request作用域
		return "bs/books";
		//spring.mvc.view.suffix=.jsp
		//spring.mvc.view.prefix=/WEB-INF/jsp/
	}
	
	@GetMapping("del")
	public String del(@RequestParam("name") String name,Book book) {
		books.remove(new Book(name,0,null));
		return "redirect:all";//重定向到all response.sendRedirect()
	}
}
